from django import http


class Code():
    def __init__(self, state, msg):
        self.state = state
        self.msg = msg


class Res:
    OK = Code(state=0, msg="OK")
    Warn = Code(state=1, msg="Warn")
    UserUnvalid = Code(state=1, msg="cookie未认证")
    ParamErr = Code(state=400, msg="不合法的参数")
    Error = Code(state=500, msg="系统繁忙")

    @classmethod
    def Json(cls, code: Code, **kwargs):
        data = {
            "state": code.state,
            "msg": code.msg,
            **kwargs
        }
        res = http.JsonResponse(data, safe=False)
        res.headers['Access-Control-Allow-Origin'] = "http://localhost:8081"
        return res

    @classmethod
    def Image(cls, file=None):
        if file:
            res = http.FileResponse(file)
        else:
            res = http.FileResponse()
        res['Content-Type'] = 'image/*'
        return res
